<?php
// Copyright (c) 2009, 백괴사전 all rights reserved.
// Distributed under GNU General Public License v3.
// Read LICENSE for more information.


function UP_JosaFunction_GetFinalJamoType($upxlTargetStr,$upxlIsRieulBln=false) {
	$upxlLengthInt=strlen($upxlTargetStr)-1;
	for($upxlLoopIndexInt=0;$upxlLoopIndexInt<=$upxlLengthInt;$upxlLoopIndexInt++) {
		if((ord($upxlTargetStr[$upxlLengthInt-$upxlLoopIndexInt])>>6)!==2)
			break;
	}
	$upxlLastChar=substr($upxlTargetStr,0-(1+$upxlLoopIndexInt));
	unset($upxlLengthInt,$upxlLoopIndexInt);
	
	if(Hangul::IsHangul($upxlLastChar)===false)
		return 2;
	
	$upxlLastCharInt=Hangul::Disorganize(UTF8::Char2Int($upxlLastChar));
	
	if($upxlLastCharInt[3]===0)
		$upxlRtnInt=0;
	else {
		if($upxlLastCharInt[3]===8 && $upxlIsRieulBln===true)
			$upxlRtnInt=0;
		else
			$upxlRtnInt=1;
	}
	unset($upxlLastCharInt);
	
	return $upxlRtnInt;
}

function UP_JosaFunction_Magic(&$upxgMagicWordsArr,$upxlLangCodeStr) {
	$magicWords['을를']=array(0,'을를');
	$magicWords['이가']=array(0,'이가');
	$magicWords['은는']=array(0,'은는');
	$magicWords['이']=array(0,'이');
	$magicWords['과와']=array(0,'과와');
	$magicWords['아야']=array(0,'아야');
	$magicWords['으']=array(0,'으');
	
	return true;
}

function UP_JosaFunction_EulReul(&$upxgWikiParserObj,$upxlTargetStr) {
	switch(UP_JosaFunction_GetFinalJamoType($upxlTargetStr)) {
		case 0:
			return '를';
			break;
		case 1:
			return '을';
			break;
		case 2:
			return '을(를)';
			break;
	}
}

function UP_JosaFunction_IGa(&$upxgWikiParserObj,$upxlTargetStr) {
	switch(UP_JosaFunction_GetFinalJamoType($upxlTargetStr)) {
		case 0:
			return '가';
			break;
		case 1:
			return '이';
			break;
		case 2:
			return '이(가)';
			break;
	}
}

function UP_JosaFunction_I(&$upxgWikiParserObj,$upxlTargetStr) {
	switch(UP_JosaFunction_GetFinalJamoType($upxlTargetStr)) {
		case 0:
			return '';
			break;
		case 1:
			return '이';
			break;
		case 2:
			return '(이)';
			break;
	}
}

function UP_JosaFunction_EunNeun(&$upxgWikiParserObj,$upxlTargetStr) {
	switch(UP_JosaFunction_GetFinalJamoType($upxlTargetStr)) {
		case 0:
			return '는';
			break;
		case 1:
			return '은';
			break;
		case 2:
			return '은(는)';
			break;
	}
}

function UP_JosaFunction_GwaWa(&$upxgWikiParserObj,$upxlTargetStr) {
	switch(UP_JosaFunction_GetFinalJamoType($upxlTargetStr)) {
		case 0:
			return '와';
			break;
		case 1:
			return '과';
			break;
		case 2:
			return '과(와)';
			break;
	}
}

function UP_JosaFunction_AYa(&$upxgWikiParserObj,$upxlTargetStr) {
	switch(UP_JosaFunction_GetFinalJamoType($upxlTargetStr)) {
		case 0:
			return '야';
			break;
		case 1:
			return '아';
			break;
		case 2:
			return '아(야)';
			break;
	}
}

function UP_JosaFunction_Eu(&$upxgWikiParserObj,$upxlTargetStr) {
	switch(UP_JosaFunction_GetFinalJamoType($upxlTargetStr,true)) {
		case 0:
			return '';
			break;
		case 1:
			return '으';
			break;
		case 2:
			return '(으)';
			break;
	}
}

?>